<?php
/**
 * Created by PhpStorm.
 * User: fengzi
 * Date: 2019/6/3
 * Time: 14:34
 */

namespace app\vehicle\logic;
use app\common\model\Wish;
use app\common\model\User;
use app\common\model\WishLog;
use app\common\model\WishImg;
class wishLogic
{
    public function getList(){
        $where[] = ['status','eq',1];
        $where[] = ['use_time','<',time()];
        $where[] = ['end_time','>',time()];
        $list = Wish::where($where)->order('create_time desc')->limit(3)->select();
        return $list;
    }
    public function getArticleList($data,$user_id){
        $time = time();
        $where[] = ['status','eq',1];
//        $where[] = ['use_time','<',$time];
//        $where[] = ['end_time','>',$time];
        if($data['status']==2){
            $where[] = ['order_status','eq',0];
        }else if($data['status']==3){
            $where[] = ['order_status','eq',1];
        }
        $limit = $data['limit']?$data['limit']:1000;
        $data['keyword'] && $where[] = ['title','like','%'.$data['keyword'].'%'];
        $data['street_name'] && $where[] = ['street_name','like','%'.$data['street_name'].'%'];
        $list = Wish::where($where)->order('order_status asc,create_time desc')->page($data['page'],$limit)->select();
        foreach($list as $k=>$v){
            $list[$k]['is_wish'] = WishLog::where('wish_id','eq',$v['id'])->where('user_id','eq',$user_id)->count();
        }
        return $list;
    }
    public function getArticleInfo($data,$user_id){
        Wish::where('id','eq',$data['id'])->setInc('read_num',1);
        $info = Wish::get($data['id']);
        $info['count_num'] = WishLog::where('wish_id','eq',$data['id'])->count();
        if($info['count_num'] && $info['number']){
            $info['progress'] = number_format($info['count_num']/$info['number']*100,1);
        }else{
            $info['progress'] = 0;
        }
        if($info['order_status']==1) {
            $info['apply_time'] = date('Y-m-d', WishLog::where('wish_id', $data['id'])->value('create_time'));
            $suc_time = WishLog::where('wish_id', $data['id'])->value('suc_time');
            $info['suc_time'] = $suc_time?date('Y-m-d', $suc_time):'';
            $cover_time = WishLog::where('wish_id', $data['id'])->value('cover_time');
            $info['cover_time'] = $cover_time?date('Y-m-d', $cover_time):'';
        }else{
            $info['apply_time'] = '';
            $info['suc_time'] = '';
            $info['cover_time'] = '';
        }
        $info['days'] =  ceil((strtotime($info['end_time']) - time() )/86400);
        $info['is_wish'] = WishLog::where('wish_id','eq',$data['id'])->where('user_id','eq',$user_id)->count();
        $info['use_integral'] = User::where('id','eq',$user_id)->value('integral');
        $info['imgs'] = WishImg::where('wid','eq',$data['id'])->select();
        return $info;
    }

    public function addWish($data,$user){

        $insert['sid'] = $user['team_admin'];
        $insert['user_id'] = $user['id'];
        $insert['title']  = $data['title'];
        $insert['thumb'] = $data['thumb'][0];
        $insert['realname']  = $data['realname'];
        $insert['mobile']  = $data['mobile'];
        $insert['area']  = $data['area'];
        $insert['street_name']  = $data['street_name'];
        $insert['village']  = $data['village'];
        $insert['v_mobile']  = $data['v_mobile'];
        $insert['address']  = $data['address'];
        $insert['content']  = $data['content'];
        $insert['use_time']  = strtotime($data['use_time']);
        $insert['end_time']  = strtotime($data['end_time']);
        $insert['create_time']  = time();
        $id = Wish::insertGetId($insert);
        foreach($data['thumb'] as $k=>$v){
            WishImg::insert(['wid'=>$id,'thumb'=>$v]);
        }
        return 1;
    }
    public function getMyUserWish($user_id){
        $list = Wish::where('user_id',$user_id)->select();
        foreach($list as $k=>$v){
            $suc_time = WishLog::where('wish_id',$v['id'])->value('suc_time');
            $list[$k]['suc_time'] = $suc_time?date('Y-m-d',$suc_time):'';
            $cover_time = WishLog::where('wish_id',$v['id'])->value('cover_time');
            $list[$k]['cover_time'] = $cover_time?date('Y-m-d',$cover_time):'';
        }
        return $list;
    }
    public function setUserWish($user_id){
        return WishLog::alias('as wl')->join('wish w','w.id=wl.wish_id')->where('wl.user_id',$user_id)->field('w.*,wl.suc_time,wl.cover_time')->select();
    }
    public function sucWish($data){
        $result = WishLog::where('wish_id',$data['id'])->update(['cover_time'=>time()]);
        return $result;
    }
    public function sucWishs($data){
        $result = WishLog::where('wish_id',$data['id'])->update(['suc_time'=>time(),'prove'=>$data['prove']]);
        return $result;
    }
    public function setWish($data,$user_id){
        $info = Wish::get($data['id']);
        if($info['order_status']==1){
            return '该心愿已认领完';
        }
        $insert['realname'] = $data['realname'];
        $insert['mobile'] = $data['mobile'];
        $insert['user_id'] = $user_id;
        $insert['wish_id'] = $data['id'];
        $insert['create_time'] = time();
        $res = WishLog::insert($insert);
        Wish::where('id','eq',$data['id'])->update(['order_status'=>1]);
        return 1;
    }
}